package
{
	import com.D5Power.Editor.D5Menu;
	import com.D5Power.Editor.Demo;
	import com.D5Power.Editor.EditorWindow.BuildingAttr;
	import com.D5Power.Editor.EditorWindow.CharacterLinker;
	import com.D5Power.Editor.EditorWindow.CharacterPosConfiger;
	import com.D5Power.Editor.EditorWindow.MapAttr;
	import com.D5Power.Editor.EditorWindow.MapConf;
	import com.D5Power.Editor.EditorWindow.MapCut;
	import com.D5Power.Editor.EditorWindow.NPCAttr;
	import com.D5Power.Editor.EditorWindow.ObjectAttr;
	import com.D5Power.Editor.EditorWindow.RoadPointAttr;
	import com.D5Power.Editor.Event.ClickObjectEvent;
	import com.D5Power.Editor.LibResource;
	import com.D5Power.Editor.LuaEditor;
	import com.D5Power.Editor.MapProperty;
	import com.D5Power.Editor.MapShower;
	import com.D5Power.Editor.ProViewer;
	import com.D5Power.Editor.Shower.MapTileShower;
	import com.D5Power.Editor.SmallMap;
	import com.D5Power.Editor.ToolBox;
	import com.D5Power.Editor.config.MouseStatus;
	import com.D5Power.utils.CharacterData;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NativeWindowDisplayStateEvent;
	import flash.filesystem.File;
	import flash.net.URLRequest;
	import flash.system.System;
	import flash.ui.Mouse;
	
	import org.aswing.AsWingManager;
	import org.aswing.BorderLayout;
	import org.aswing.Container;
	import org.aswing.JButton;
	import org.aswing.JFrame;
	import org.aswing.JLabel;
	import org.aswing.JOptionPane;
	import org.aswing.JPanel;
	import org.aswing.JTabbedPane;
	import org.aswing.JWindow;
	import org.aswing.LoadIcon;
	import org.aswing.SoftBoxLayout;
	import org.aswing.border.LineBorder;
	import org.aswing.event.FrameEvent;
	
	[SWF(width="1000",height="680",frameRate="30",backgroundColor="#000000")]
	public class D5RpgEditor extends Sprite
	{
		/**
		 * 菜单容器
		 */
		private var menuContainer:D5Menu;
		
		/**
		 * 工具容器
		 */ 
		private var toolContainer:JPanel;
		
		/**
		 * 左侧容器
		 */ 
		private var leftContainer:JPanel;
		
		/**
		 * 地图显示容器
		 */ 
		private var mainContainer:JPanel;
		
		/**
		 * 状态栏
		 */ 
		private var statusContainer:JPanel;
		
		/**
		 * 主容器
		 */ 
		private var mainWindow:JWindow;
		
		/**
		 * 地图显示器
		 */ 
		private var shower:MapShower;
		
		/**
		 * 小地图
		 */ 
		private var smallmap:SmallMap;
		
		/**
		 * 当前地图属性
		 */ 
		private var map_prop:MapProperty;
		
		private var pro_shower:ProViewer;
		
		/**
		 * 可用建筑资源库
		 */ 
		private var resource:LibResource;
		
		private var resource_npc:LibResource;
		
		
		/**
		 * 现在正在显示的属性视窗（每次只能显示一个属性视窗）
		 */ 
		private var nowAttr:ObjectAttr;
		
		/**
		 * 演示用场景
		 */ 
		private var demo:Demo;
		
		private var _mouseStatus:String='';
		
		private var _mouse:Bitmap;
		
		public static var W:uint = 800;
		public static var H:uint = 600;
		
		public function D5RpgEditor()
		{
			// 不启用资源库
			Global.LIBNAME='';
			Global.userdata = new CharacterData();
			init();
		}
		
		public function init():void
		{
			addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGE,onResize);
			D5Power.W=stage.stageWidth;
			D5Power.H=stage.stageHeight;
			D5Power.stage = stage;
			
			AsWingManager.initAsStandard(stage);
			System.useCodePage=true;
			
			mainWindow = new JWindow();
			
			
			menuContainer = new D5Menu(this);
			toolContainer = new JPanel();
			leftContainer = new JPanel(new SoftBoxLayout(SoftBoxLayout.Y_AXIS));
			mainContainer = new JPanel();
			statusContainer = new JPanel();
			
			// 得到窗口容器
			var pane:Container = mainWindow.getContentPane();
			pane.setLayout(new BorderLayout());
			
			var top:JPanel = new JPanel(new SoftBoxLayout(SoftBoxLayout.Y_AXIS));
			top.append(menuContainer);
			top.append(toolContainer);
			pane.append(top, BorderLayout.NORTH);
			
			pane.append(leftContainer,BorderLayout.WEST);
			pane.append(mainContainer,BorderLayout.CENTER);
			pane.append(statusContainer,BorderLayout.SOUTH);
			
			
			// 开始设置样式
			menuContainer.setOpaque(true);
			leftContainer.setOpaque(true);
			mainContainer.setOpaque(true);
			toolContainer.setOpaque(true);
			statusContainer.setOpaque(true);
			
			
			mainWindow.setSizeWH(D5Power.W,D5Power.H);
			mainWindow.show();
			setTop();
			setLeft();
			
		}
		
		public function set mouseStatus(s:String):void
		{
			var cursor:String = '';
			
			if(s==_mouseStatus) s='';
			
			switch(s)
			{
				case '':
					
					break;
				case MouseStatus.DRAW_OVER:
					cursor = 'drawOver.png';
					break;
			}
			
			_mouseStatus = s;
			
			if(cursor!='')
			{
				var l:Loader = new Loader();
				l.contentLoaderInfo.addEventListener(Event.COMPLETE,onCursorLoaded);
				l.load(new URLRequest('EditorUI/cursor/'+cursor));
			}
		}
		
		public function get mouseStatus():String
		{
			return _mouseStatus;
		}
		
		public function get Window():JWindow
		{
			return mainWindow;
		}
		
		public function get mapShowerBox():JPanel
		{
			return mainContainer;
		}
		
		/**
		 * 地图显示器
		 */ 
		public function get Shower():MapShower
		{
			return shower;
		}
		
		/**
		 * 小地图
		 */ 
		public function get Smallmap():SmallMap
		{
			return smallmap;
		}
		
		/**
		 * 顶部菜单
		 */ 
		public function get Menu():D5Menu
		{
			return menuContainer;
		}
		
		public function get MapProp():MapProperty
		{
			return map_prop;
		}
		
		public function get proShower():ProViewer
		{
			return pro_shower;
		}
		
		/**
		 * 运行地图
		 */ 
		public function Run():void
		{
			shower.PAUSE();
			if(Menu.filename=='')
			{
				msg('提示','您还没有进行任何的地图编辑，或者还没有保存当前的操作。请先保存后再运行地图。');
				return;
			}
			
			Global.LOADWAIT.splice(0,Global.LOADWAIT.length);
			demo = new Demo(Menu.filename,stage);
			var demoFrame:JFrame=new JFrame(mainWindow,'运行地图',true);
			demoFrame.setSizeWH(Global.W,Global.H);
			demoFrame.getContentPane().addChild(demo);
			demoFrame.show();
			demoFrame.addEventListener(FrameEvent.FRAME_CLOSING,onRunClose);
		}
		
		/**
		 * 释放属性窗
		 */ 
		public function releaseAttrWin():void
		{
			nowAttr=null;
		}
		
		public function about():void
		{
			msg('关于','D5Power RPG地图编辑器\n' +
				'--------------------------\n' +
				'官方网站：www.d5power.com\n' +
				'开发讨论社区：www.webgamei.com\n' +
				'技术支持：D5PowerStudio@gmail.com\n' +
				'--------------------------\n' +
				'Copyright [c] 2000-2011 By D5Power.com Allrights Reserved.');
		}
		
		public function msg(title:String,content:String):void
		{
			JOptionPane.showMessageDialog(title,content);
		}
		
		public function newOrEditMap(act:uint):void
		{
			var win:MapAttr = new MapAttr(act,this,null);
			win.show();
		}
		
		public function MapConfig():void
		{
			var win:MapConf = new MapConf(this);
			win.show();
		}
		
		public function cutMapTool():void
		{
			var win:MapCut = new MapCut(this);
			win.show();
		}
		
		public function CharacterPos():void
		{
			var win:CharacterPosConfiger = new CharacterPosConfiger(this);
			win.show();
		}
		
		public function CharacterFix():void
		{
			var win:CharacterLinker = new CharacterLinker(this);
			win.show();
		}
		
		public function startMap(config:String=''):void
		{
			if(shower!=null) clearMap();
			if(config=='')
			{
				// 新建地图，自动生成配置文件
				var tempShower:MapShower = new MapShower('',stage,this);
				config = 'map'+D5Power.mapid;
				var temp_file:File = new File(File.applicationDirectory.resolvePath('config/'+config+'.d5').nativePath);
				menuContainer.onSaveAs(temp_file,tempShower.Configer);
				tempShower.clear();
				tempShower = null;
			}
			
			
			shower = new MapShower(config,stage,this);
			shower.x = 10;
			mainContainer.addChild(shower);
			
			shower.addEventListener(ClickObjectEvent.CLICK,onclickObj);
		}
		
		public function startSmallMap():void
		{
			if(smallmap==null) return;
			
			smallmap.mapShower=shower;
			smallmap.loadSmallMap();
		}
		
		public function flushMap():void
		{
			
		}
		
		public function showResource():void
		{
			if(D5Power.mapid==-1)
			{
				msg('提示','地图数据尚未初始化！');
				return;
			}
			resource.build(D5Power.mapid);
			resource_npc.build(D5Power.mapid);
		}
		
		private function setTop():void
		{
			var toolBox:ToolBox = new ToolBox(null,this);
			toolContainer.append(toolBox);
		}
		
		private function setLeft():void
		{
			leftContainer.setPreferredWidth(D5Power.SMALLMAPSIZE);
			
			smallmap = new SmallMap(this);
			leftContainer.append(smallmap);
			
			var tabBox:JTabbedPane = new JTabbedPane();
			
			resource = new LibResource(LibResource.MAP,null,this);
			resource_npc = new LibResource(LibResource.CHARACTER,null,this);
			
			tabBox.appendTab(resource,'建筑');
			tabBox.appendTab(resource_npc,'NPC');
			
			leftContainer.append(tabBox);
			
			map_prop = new MapProperty(null,this);
			leftContainer.append(map_prop);
			
			pro_shower = new ProViewer();
			leftContainer.append(pro_shower);
			
		}
		
		private function onResize(e:*):void
		{
			mainWindow.setSizeWH(stage.nativeWindow.width,stage.nativeWindow.height);
		}
		
		/**
		 * 清空地图
		 */ 
		private function clearMap():void
		{
			mainContainer.removeChild(shower);
			
			if(shower!=null)
			{
				shower.clear();
				shower = null;
			}
			
			resource.removeAll();
			resource_npc.removeAll();
			
			Global.CLEAR();
		}
		
		
		/**
		 * 停止地图运行
		 */ 
		private function onRunClose(e:FrameEvent):void
		{
			demo.clear();
			var target:JFrame = e.target as JFrame;
			target.removeEventListener(FrameEvent.FRAME_CLOSING,onRunClose);
			target.getContentPane().removeChild(demo);
			target.closeReleased();
			
			shower.PAUSE();
		}
		
		private function onCursorLoaded(e:Event):void
		{
			var loader:Loader = (e.target as LoaderInfo).loader;
			_mouse = loader.content as Bitmap;
			Mouse.hide();
			
			mainWindow.addChild(_mouse);
			updateCursorPos(stage.mouseX,stage.mouseY);
			loader.unload();
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE,updateCursor);
		}
		
		private function updateCursor(e:MouseEvent):void
		{
			if(_mouseStatus=='')
			{
				// 空鼠标状态 删除事件
				stage.removeEventListener(MouseEvent.MOUSE_MOVE,updateCursor);
				mainWindow.removeChild(_mouse);
				_mouse.bitmapData.dispose();
				_mouse=null;
				Mouse.show();
				return;
			}
			
			updateCursorPos(e.stageX,e.stageY);
			
			e.updateAfterEvent();
		}
		/**
		 * 更新鼠标指针的坐标
		 */ 
		private function updateCursorPos(sx:Number,sy:Number):void
		{
			if(_mouseStatus=='' || _mouse==null) return;
			
			switch(_mouseStatus)
			{
				case MouseStatus.DRAW_OVER:
					sy = sy-_mouse.height;
					break;
			}
			
			_mouse.x = sx;
			_mouse.y = sy;
		}
		
		private function onclickObj(e:ClickObjectEvent):void
		{
			if(nowAttr!=null)
			{
				nowAttr.onMouseEvent(e);
				return;
			}
			
			switch(e.click_target.objectName)
			{
				case 'BuildingObject':
					nowAttr = new BuildingAttr(this,e.click_target);
					break;
				case 'NCharacterObject':
					nowAttr = new NPCAttr(this,e.click_target);
					break;
				case 'RoadPoint':
					nowAttr = new RoadPointAttr(this,e.click_target);
					break;
			}
			nowAttr.show();
		}
	}
}